<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            :clickModalClose="true"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="84px" :rules="formRules" label-position="top">
                <el-form-item label="客户名称" prop="customerName">
                    <el-input v-model="formData.customerName" placeholder="请输入客户名称" />
                </el-form-item>
                <el-form-item label="客户电话" prop="customerCall">
                    <el-input v-model="formData.customerCall" placeholder="请输入客户电话" />
                </el-form-item>
                <el-form-item label="身份证号" prop="idNumber">
                    <el-input v-model="formData.idNumber" placeholder="请输入身份证号" />
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-radio-group v-model="formData.sex" placeholder="性别">
                        <el-radio
                            v-for="(item, index) in dictData.sex"
                            :key="index"
                            :label="item.value"
                            :disabled="!item.status"
                        >
                            {{ item.name }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="住址" prop="address">
                    <el-input v-model="formData.address" placeholder="请输入住址" />
                </el-form-item>
                <el-form-item label="银行" prop="bank">
                    <el-input v-model="formData.bank" placeholder="请输入银行" />
                </el-form-item>
                <el-form-item label="欠款金额" prop="amountDue">
                    <el-input v-model="formData.amountDue" placeholder="请输入欠款金额" />
                </el-form-item>
                <el-form-item label="案件类型" prop="caseType">
                    <el-radio-group v-model="formData.caseType">
                        <el-radio
                            v-for="(item, index) in dictData.case_type"
                            :key="index"
                            :label="item.value"
                            :disabled="!item.status"
                        >
                            {{ item.name }}
                        </el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input v-model="formData.remark" placeholder="请输入备注" />
                </el-form-item>
                <el-form-item label="分配调解员" prop="courtClerkId">
                    <el-select class="flex-1" v-model="formData.courtClerkId" placeholder="请选择调解员">
<!--                        <el-option label="请选择字典生成" value="" />-->
                        <el-option v-for="(item, index) in optionsData.courtClerk" :key="index" :label="item.username" :value="item.id"/>
                    </el-select>
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'
import {customerEdit, customerAdd, customerDetail, allCourtClerk} from '@/api/customer'
import Popup from '@/components/popup/index.vue'
import feedback from '@/utils/feedback'
import type { PropType } from 'vue'
import {useDictOptions} from "@/hooks/useDictOptions";

defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑客户' : '新增客户'
})

const formData = reactive({
    customerNumber: '',
    customerName: '',
    customerCall: '',
    idNumber: '',
    sex: '1',
    address: '',
    bank: '',
    amountDue: '',
    caseType: '',
    remark: '',
    courtClerkId: '',
})

const { optionsData } = useDictOptions<{
    courtClerk: any[]
}>({
    courtClerk: {
        api: allCourtClerk
    }
})

const formRules = {
    customerName: [
        {
            required: true,
            message: '请输入客户姓名',
            trigger: ['blur']
        }
    ],
    customerCall: [
        {
            required: true,
            message: '请输入客户电话',
            trigger: ['blur']
        }
    ],
    idNumber: [
        {
            required: true,
            message: '请输入身份证号',
            trigger: ['blur']
        }
    ],
    sex: [
        {
            required: true,
            message: '请选择性别',
            trigger: ['blur']
        }
    ],
    address: [
        {
            required: true,
            message: '请输入住址',
            trigger: ['blur']
        }
    ],
    bank: [
        {
            required: true,
            message: '请输入银行',
            trigger: ['blur']
        }
    ],
    amountDue: [
        {
            required: true,
            message: '请输入欠款金额',
            trigger: ['blur']
        }
    ],
    caseType: [
        {
            required: true,
            message: '请选择案件类型',
            trigger: ['blur']
        }
    ],
    remark: [
        {
            required: true,
            message: '请输入备注',
            trigger: ['blur']
        }
    ],
    courtClerkId: [
        {
            required: true,
            message: '请选择调解员',
            trigger: ['blur']
        }
    ],
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    const data: any = { ...formData }
    mode.value == 'edit' ? await customerEdit(data) : await customerAdd(data)
    popupRef.value?.close()
    feedback.msgSuccess('操作成功')
    emit('success')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<string, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            if (key == 'caseType') {
                formData[key] = String(data[key])
            } else {
                formData[key] = data[key]
            }
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await customerDetail({
        customerNumber: row.customerNumber
    })
    setFormData(data)
}

const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
